﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using DotNetOpenAuth.ApplicationBlock;
using DotNetOpenAuth.OAuth.Messages;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.AttributeExchange;
using DotNetOpenAuth.OpenId.RelyingParty;

using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OpenId.Extensions.OAuth;
using OpenIdRelyingPartyWebForms;

using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.Linq;

namespace YahooSamples.login
{
    public partial class Member : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(State.YahooAccessToken))
            {
                string yql = "select * from social.profile where guid=me";
                XDocument result = YahooConsumer.YQL(Global.YahooWebConsumer, State.YahooAccessToken, yql);
                XNamespace n1 = "http://www.yahooapis.com/v1/base.rng";
                XNamespace n2 = "http://social.yahooapis.com/v1/schema.rng";

                this.lnickname.Text = result.Root.Element("results").Element(n2 + "profile").Element(n2 + "nickname").Value;
                this.lgender.Text = result.Root.Element("results").Element(n2 + "profile").Element(n2 + "gender").Value;
                this.lemail.Text = State.FetchResponse.GetAttributeValue("http://axschema.org/contact/email");
                this.llanguage.Text = State.FetchResponse.GetAttributeValue("http://axschema.org/pref/language");
                this.lfullname.Text = State.FetchResponse.GetAttributeValue("http://axschema.org/namePerson");

                this.photo.ImageUrl = result.Root.Element("results").Element(n2 + "profile").Element(n2 + "image").Element(n2 + "imageUrl").Value;
                this.photo.Height = int.Parse(result.Root.Element("results").Element(n2 + "profile").Element(n2 + "image").Element(n2 + "height").Value) / 2;
                this.photo.Width = int.Parse(result.Root.Element("results").Element(n2 + "profile").Element(n2 + "image").Element(n2 + "width").Value) / 2;

                State.UserGUID = result.Root.Element("results").Element(n2 + "profile").Element(n2 + "guid").Value;
            }
            else
            {
                Response.Redirect("~/login/Default.aspx");
            }
        }
    }
}